package com.tarena.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tarena.entity.Booking;
import com.tarena.entity.Meettingaddress;
import com.tarena.service.MeettingaddressService;

import net.sf.json.JSONObject;
@Controller
@RequestMapping("/meettingaddress")

public class MeettingaddressController {
	@Resource
	private MeettingaddressService meettingaddressService;
	private PrintWriter out;
	
	@RequestMapping("/saveMeettingaddress.do")
	public void save(Meettingaddress meettingaddress, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("save");
		boolean save = meettingaddressService.save(meettingaddress);
		out = res.getWriter();
		out = res.getWriter();
		if (save) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	@RequestMapping("/deleteMeettingaddress.do")
	public void delete(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("delete");
		boolean delete = meettingaddressService.delete(Integer.parseInt(req.getParameter("id")));
		out = res.getWriter();
		if (delete) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	 @RequestMapping("/updateMeettingaddress.do")
	 public void update(Meettingaddress meettingaddress, HttpServletRequest req, HttpServletResponse res)
				throws Exception {
			out = res.getWriter();
			try {
				meettingaddressService.update(meettingaddress);
				out.write("1");
				out.flush();
				out.close();
			} catch (Exception e) {
				out.write("0");
				out.flush();
				out.close();
			}
			
		}
	 @RequestMapping("/findMeettingaddress")
	 public String findAllMeettingaddress(Model mode) {
			List<Meettingaddress> list = meettingaddressService.findAllMeettingaddress();
			mode.addAttribute("meettingaddresss", list);
			return "meettingaddress/meettingaddress_list";
		}
	 @RequestMapping("/findById.do")
		public String findById(HttpServletRequest req, HttpServletResponse res)
				throws Exception {
			List<Meettingaddress> list = meettingaddressService.findById(Integer.parseInt(req
					.getParameter("id")));
			req.setAttribute("meettingaddresss", list);
			return "meettingaddress/meettingaddress_list";
		}
	 @RequestMapping("/findByPage.do")
		public void findByPage(Meettingaddress meettingaddress, HttpServletRequest req, HttpServletResponse res)
				throws Exception {
			System.out.println("findByPage");
			String page = req.getParameter("page");
			String rows = req.getParameter("rows");
			System.out.println(page);
			
			List<Meettingaddress> list = null;
			if (null != meettingaddress.getAddress() && !"".equals(meettingaddress.getAddress())) {
				if (null != meettingaddress.getAddress() && !"".equals(meettingaddress.getAddress())) {
					meettingaddress.setAddress("%" + meettingaddress.getAddress() + "%");
				}
				list = meettingaddressService.findByPage(
					Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
					Integer.parseInt(rows), meettingaddress);
			} else {
				list = meettingaddressService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows));
			}
			System.out.println(list.toString());
			Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
			jsonMap.put("total", meettingaddressService.getSum());//total键 存放总记录数，必须的  

	        jsonMap.put("rows", list);//rows键 存放每页记录 list  

	        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
	        out = res.getWriter();
			out.write(result.toString());
			System.out.println(result.toString());
			out.flush();
			out.close();
			
			
		}
}
